package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.mapper.MaintenanceRecordMapper;
import com.j283_5.wisdom_park.pojo.Maintenance;
import com.j283_5.wisdom_park.pojo.MaintenanceDelivery;
import com.j283_5.wisdom_park.pojo.MaintenanceRecord;
import com.j283_5.wisdom_park.service.MaintenanceRecordService;
import com.j283_5.wisdom_park.vo.maintenance_management.MaintenanceDeliveryVo;
import com.j283_5.wisdom_park.vo.maintenance_management.MaintenanceVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
@Transactional
public class MaintenanceRecordServiceImpl extends ServiceImpl<MaintenanceRecordMapper, MaintenanceRecord> implements MaintenanceRecordService {

    @Resource
    private MaintenanceRecordMapper maintenanceRecordMapper;
    @Override
    public List<MaintenanceVo> queryAllMaintenance(Page page, MaintenanceVo maintenanceVo,Integer adminId) {
        List<Maintenance> maintenanceList = maintenanceRecordMapper.queryAllMaintenance(new Maintenance(maintenanceVo), (int) ((page.getCurrent() - 1) * page.getSize()), (int) (page.getSize()),adminId);
        List<MaintenanceVo> maintenanceVoList = new ArrayList();
        maintenanceList.forEach((v)->{
            maintenanceVoList.add(new MaintenanceVo(v));
        });
        return maintenanceVoList;
    }

    @Override
    public Integer queryMaintenanceCount(MaintenanceVo maintenanceVo,Integer adminId) {
        return maintenanceRecordMapper.queryMaintenanceCount(new Maintenance(maintenanceVo),adminId);
    }

    @Override
    public boolean deleteMaintenance(ArrayList maintenanceIdList) {
        int row = maintenanceRecordMapper.deleteMaintenance(maintenanceIdList);
        if (row == maintenanceIdList.size()){
            return true;
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return false;
    }

    @Override
    public MaintenanceDeliveryVo queryMaintenanceDelivery(Integer mrecId) {
        return new MaintenanceDeliveryVo(maintenanceRecordMapper.queryMaintenanceDelivery(mrecId));
    }

    @Override
    public boolean maintenanceDelivery(Integer mrecId, Integer adminId) {
        if (maintenanceRecordMapper.maintenanceDelivery(mrecId, adminId) == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateMaintenanceDelivery(MaintenanceDeliveryVo maintenanceDeliveryVo) {
        if (2 == maintenanceRecordMapper.updateMaintenanceDelivery(new MaintenanceDelivery(maintenanceDeliveryVo))){
            return true;
        }
        return false;
    }

    @Override
    public boolean addMaintenanceDelivery(MaintenanceDeliveryVo maintenanceDeliveryVo, Integer wrecId) {
        if (1 == maintenanceRecordMapper.addMaintenanceDelivery(new MaintenanceDelivery(maintenanceDeliveryVo), wrecId)){
            return true;
        }
        return false;
    }
}
